package com.ibeeking.found.common.utils;

import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.ibeeking.nematos.utils.result.ResponseResult;
import feign.Response;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Description
 * @Author ibeeking
 * @Date 2021-01-16
 **/
public class DataUtils {
    public static final String LTE_ZERO_EXCEPTION_MSG = "数字必须大于0!";
    private static Pattern humpPattern = Pattern.compile("[A-Z]");
    private static String LETTER_DIGIT_REGEX = "^[a-z0-9A-Z]+$";
    private static String CHINESE_REGEX = "[\u4e00-\u9fa5]";

    /**
     * condition为true抛出异常
     *
     * @param condition
     * @param exceptionMsg
     */
    public static void trueThrowException(Boolean condition, String exceptionMsg) {
        if (condition == null || StringUtils.isBlank(exceptionMsg)) {
            return;
        }
        if (condition) {
            throw BusinessException.create(exceptionMsg);
        }
    }

    public static String methodMsg(String methodName, Object methodParam, Object methodReturn, String msg) {
        String methodParamStr = null;
        String methodReturnStr = null;
        try {
            methodParamStr = JsonUtils.toJsonStr(methodParam);
            methodReturnStr = JsonUtils.toJsonStr(methodReturn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return com.ibeeking.nematos.utils.data.StringUtils.format("方法:%s,参数:%s,返回:%s,msg:%s", methodName, methodParamStr, methodReturnStr, msg);
    }

    public static String methodMsg(String methodName, Object methodParam, ResponseResult res) {
        String methodParamStr = null;
        String methodReturnStr = null;
        try {
            methodParamStr = JsonUtils.toJsonStr(methodParam);
            methodReturnStr = JsonUtils.toJsonStr(res.getData());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return methodMsg(methodName, methodParamStr, methodReturnStr, res.getMsg());
    }


    /**
     * condition为false抛出异常
     *
     * @param condition
     * @param exceptionMsg
     */
    public static void falseThrowException(Boolean condition, String exceptionMsg) {
        if (StringUtils.isBlank(exceptionMsg)) {
            return;
        }
        if (condition == null || !condition) {
            throw BusinessException.create(exceptionMsg);
        }
    }

    /**
     * 存在数字小于0,抛出异常,null不抛异常
     *
     * @param nums
     */
    public static void checkLteZeroException(Long... nums) {
        if (nums == null) {
            return;
        }
        for (Long num : nums) {
            if (num == null) {
                continue;
            }
            if (num <= 0L) {
                throw BusinessException.create(LTE_ZERO_EXCEPTION_MSG);
            }
        }
    }

    /**
     * 存在数字小于0,抛出异常,null不抛异常
     *
     * @param nums
     */
    public static void checkLteZeroException(Integer... nums) {
        if (nums == null) {
            return;
        }
        for (Integer num : nums) {
            if (num == null) {
                continue;
            }
            if (num <= 0) {
                throw BusinessException.create(LTE_ZERO_EXCEPTION_MSG);
            }
        }
    }

    /**
     * 存在数字小于0,抛出异常,null不抛异常
     *
     * @param nums
     */
    public static void checkLteZeroException(BigDecimal... nums) {
        if (nums == null) {
            return;
        }
        for (BigDecimal num : nums) {
            if (num == null) {
                continue;
            }
            if (num.compareTo(BigDecimal.ZERO) <= 0) {
                throw BusinessException.create(LTE_ZERO_EXCEPTION_MSG);
            }
        }
    }

    /**
     * 所有值为空时,抛出异常
     *
     * @param exceptionMsg
     * @param objs
     */
    public static void checkAllNullException(String exceptionMsg, Object... objs) {
        if (objs == null) {
            throw BusinessException.create(exceptionMsg);
        }

        int count = 0;
        for (Object obj : objs) {
            if (obj == null) {
                count++;
            }
        }
        if (count == objs.length) {
            throw BusinessException.create(exceptionMsg);
        }
    }

    /**
     * 不是所有值都为空时,抛出异常(所有值都为空时,不抛出异常)
     *
     * @param exceptionMsg
     * @param objs
     */
    public static void checkNotAllNullException(String exceptionMsg, Object... objs) {
        if (objs == null) {
            return;
        }

        int count = 0;
        for (Object obj : objs) {
            if (obj == null) {
                count++;
            }
        }
        if (count != 0 && count != objs.length) {
            throw BusinessException.create(exceptionMsg);
        }
    }

    /**
     * 根据 传入的condition true则添加
     *
     * @param collection
     * @param condition
     * @param obj
     * @param <T>
     */
    public static <T> void collectionAdd(Collection<T> collection, Boolean condition, T obj) {
        if (condition) {
            collection.add(obj);
        }
    }

    /**
     * 字段驼峰转下划线
     *
     * @param str
     * @return
     */
    public static String humpToLine(String str) {
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }


    public static Long multiplyOneHundred(Long num) {
        return multiply(num, 100L);
    }

    public static Long multiply(Long num, Long multiplyNum) {
        if (num == null || multiplyNum == null) {
            return num;
        }
        return num * multiplyNum;
    }

    public static BigDecimal divideOneHundred(BigDecimal num) {
        return divide(num, new BigDecimal(100));
    }

    public static BigDecimal divide(BigDecimal num, BigDecimal divideNum) {
        if (num == null || divideNum == null) {
            return num;
        }
        return num.divide(divideNum);
    }

    /**
     * 判断是否只包含字母数字
     *
     * @param str
     * @return
     */
    public static boolean isLetterDigit(String str) {
        if (StringUtils.isBlank(str)) {
            return false;
        }
        return str.matches(LETTER_DIGIT_REGEX);
    }

    /**
     * 字符串拼接 (null值会转为"")
     *
     * @param strs
     * @return
     */
    public static String strSplicing(String... strs) {
        if (strs == null) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        for (String str : strs) {
            sb.append(str == null ? "" : str);
        }
        return sb.toString();
    }

    /**
     * 隐藏手机号中间4位
     *
     * @param phone
     * @return
     */
    public static String hidePhone(String phone) {
        if (StringUtils.isBlank(phone)) {
            return phone;
        }
        return phone.replaceAll("(\\d{3})\\d{4}", "$1****");
    }

    public static void main(String[] args) {
    }


}
